﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dominio;
using System.Data.Objects;

namespace Servicios
{
    public abstract class GenericService<T>  where T : class, IIdentificable 
    {
        private DominioContainer _ctx = new DominioContainer();

        protected abstract ObjectSet<T> GetObjectSet(DominioContainer ctx);

        protected DominioContainer Ctx {
            get {
              _ctx = new DominioContainer();
                return _ctx;
            }
        }

        public virtual void Registrar(T t) 
        {
            GetObjectSet(_ctx).AddObject(t);
            _ctx.SaveChanges();
        }

        public virtual void Borrar(T t)
        {
            using (DominioContainer ctx = new DominioContainer())
            {
                GetObjectSet(ctx).Attach(t);
                GetObjectSet(ctx).DeleteObject(t);
                ctx.SaveChanges();
            }
        }

        public virtual IList<T> ObtenerTodos()
        {

            IList<T> ts = new List<T>();
            foreach(T t in GetObjectSet(Ctx))
            {
                ts.Add(t);
            }
            return ts;
        }

    }
}
